const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  const _ = db.command
  const getResult = await db.collection('room').where({
    'players.openid': openid,
    status: _.lt(3)
  }).get()
  if (getResult.data.length) {
    return getResult.data[0]._id
  } else {
    return null
  }
}